package com.ray.bean;

import java.io.UnsupportedEncodingException;

import com.ray.util.GUID;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 考勤设备指令
 * @author 王荣
 */
@Getter
@Setter
@ToString
public class AttendDeviceTransBean {
	private Long guid;
	//设备表记录id
	private Integer deviceId;
	//考勤机二进制指令
	private byte[] cmdParam;
	//指令类型 删除：24；添加：35
	private Integer cmdCode;
	//添加删除的用户id
	private String userId;
	
	public AttendDeviceTransBean() {
		super();
	}
	public AttendDeviceTransBean(Integer deviceId,byte[] cmdParam,Integer cmdCode,String userId) {
		this.guid = GUID.getGuid();
		this.deviceId = deviceId;
		this.cmdParam = cmdParam;
		this.cmdCode = cmdCode;
		this.userId = userId;
	}
	/** 
	 * 删除命令 
	 * @param deviceId 打卡机id 
	 * @param userId 删除用户id 
	 */
	public AttendDeviceTransBean(Integer deviceId,String userId) {
		this.guid = GUID.getGuid();
		this.deviceId = deviceId;
		this.cmdCode = 24;
		this.userId = userId;
		StringBuffer bodyBuf=new StringBuffer();
		bodyBuf.append("\020\0\0\0{\"user_id\":\"");
		bodyBuf.append(userId);
		bodyBuf.append("\"}\0");
		try {
			this.cmdParam = bodyBuf.toString().getBytes("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	/** 
	 * 新增并设置角色 
	 * @param deviceId 打卡机id
	 * @param userId 新增用户id
	 * @param userName 新增用户姓名
	 * @param role 角色： USER普通用户；MANAGER管理员
	 */
	public AttendDeviceTransBean(Integer deviceId,String userId,String userName,String role) {
		this.guid = GUID.getGuid();
		this.deviceId = deviceId;
		this.cmdCode = 35;
		this.userId = userId;
		StringBuffer bodyBuf=new StringBuffer();
	    bodyBuf.append("T\0\0\0{\"user_id\":\"");
		bodyBuf.append(userId); 
		bodyBuf.append("\",\"user_name\":\"");
		bodyBuf.append(userName);
		bodyBuf.append("\",\"user_privilege\":\"");
		bodyBuf.append(role);
		bodyBuf.append("\"}\0");
		try {
			this.cmdParam = bodyBuf.toString().getBytes("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
}
